package com.aco.party.web.controller.system;

import com.aco.party.enums.EventStatusEnum;
import com.aco.party.model.po.Events;
import com.aco.party.model.vo.EventVo;
import com.aco.party.model.vo.common.DataGridEntry;
import com.aco.party.model.vo.common.Page;
import com.aco.party.model.vo.common.PageParam;
import com.aco.party.model.vo.common.Response;
import com.aco.party.service.system.EventService;
import com.aco.party.web.command.system.EditEventCommand;
import com.aco.party.web.validator.system.EditEventValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import static com.aco.party.util.WebModelUtil.*;

/**
 * Created by WangShijun on 2016/3/3.
 */
@Controller
@RequestMapping("/system")
public class ManageEventsController extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(ManageEventsController.class);
    private EditEventValidator validator = new EditEventValidator();
    @Resource
    private EventService eventService;

    @RequestMapping(value = {"/event","/event/list"},method = RequestMethod.GET)
    public String manageUsers(Model model,HttpServletRequest request,HttpServletResponse response){
        return "system/manageEvents";
    }

    @RequestMapping(value = "/event/list",method = RequestMethod.POST)
    @ResponseBody
    public Page<EventVo> getUsers(HttpServletRequest request,DataGridEntry entry){
        Page<EventVo> users = new Page<>();
        entry.webParamConvert();
        List<EventVo> eventsList = eventService.getEventsByPage(new PageParam(entry.getOffset(),entry.getLimit()));
        users.setTotal(eventService.getEventsTotal());
        users.setRows(eventsList);
        return users;
    }

    @RequestMapping(value = "/event/edit",method = RequestMethod.GET)
    public String showEditEvent(@ModelAttribute EditEventCommand command){
        if(command.getEventId() != null){
            Events event = eventService.getByPrimaryKey(command.getEventId());
            if(event != null)
                command.toCommand(event);
        }
        return "system/editEvent";
    }

    @RequestMapping(value = "/event/edit",method = RequestMethod.POST)
    public String editEvent(Model model,@ModelAttribute EditEventCommand command,BindingResult errors){
        validator.validate(command, errors);
        if (!errors.hasErrors()) {
            Events events = new Events();
            command.toPo(events);
            events.setUpdateTime(new Date());
            eventService.update(events);
            model.addAttribute("no_error", true);
        }
        return "system/editEvent";
    }

    @RequestMapping(value = "/event/add",method = RequestMethod.GET)
    public String showAddEvent(@ModelAttribute EditEventCommand command){
        return "system/editEvent";
    }

    @RequestMapping(value = "/event/add",method = RequestMethod.POST)
    public String addEvent(Model model,@ModelAttribute EditEventCommand command,BindingResult errors,RedirectAttributes attributes){
        validator.validate(command, errors);
        if (errors.hasErrors()) {
            return "system/editEvent";
        }
        Events events = new Events();
        command.toPo(events);
        events.setCreateTime(new Date());
        events.setEventStatus(EventStatusEnum.正在进行中.getValue());
        eventService.add(events);
        attributes.addAttribute("eventId",events.getEventId());
        attributes.addFlashAttribute("no_error", true);
        return REDIRECTEDIT;
    }

    @RequestMapping(value = "/event/delete", method = RequestMethod.POST)
    @ResponseBody
    public Response del(@RequestParam(required = true) Long id) {
        logger.info("删除：" + id);
        eventService.delete(id);
        return new Response();
    }
}
